package com.example.memosystem.mapper;

import com.example.memosystem.model.User;
import org.apache.ibatis.annotations.*;

import java.util.Date;

@Mapper
public interface UserMapper {
    
    @Insert("INSERT INTO user (username, password, email, create_time, update_time) VALUES (#{username}, #{password}, #{email}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertUser(User user);

    @Select("SELECT id, username, password, email, create_time, update_time FROM user WHERE username = #{username}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "password", column = "password"),
        @Result(property = "email", column = "email"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    User findByUsername(String username);
    
    @Select("SELECT id, username, password, email, create_time, update_time FROM user WHERE id = #{id}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "password", column = "password"),
        @Result(property = "email", column = "email"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    User findById(Integer id);

    @Update("<script>UPDATE user SET " +
            "<if test='username != null'>username = #{username},</if> " +
            "<if test='password != null'>password = #{password},</if> " +
            "<if test='email != null'>email = #{email},</if> " +
            "update_time = NOW() " +
            "WHERE id = #{id}</script>")
    int updateUser(User user);
    
    @Delete("DELETE FROM user WHERE id = #{id}")
    int deleteUser(Integer id);
}